%
%   [NSx] = combineNSx(filename1, filename2)
%
%   This function loads two NSx files and it will combine them
%   together into a sinlge NSx structure in MATLAB. To merge two
%   NSx and NEV files into individual files see mergeNSxNEV. The time
%   difference between the two sets of recordings is removed. To determine
%   the time differnce between the two data files, use
%   NSx.MetaTags.DateTimeRaw or NEV.MetaTags.DateTimeRaw variables.
%
%
%   filename1:  The name of the first NSx file. This input is optional. In
%               its absense, a dialog will open and will prompt the user to
%               select an NSx file.
%               (OPTIONAL)
%
%   filename2:  The name of the second NSx file. This input is also
%               optional. In its absense, a dialog will open and will
%               prompt the user to select an NSx file.
%               (OPTIONAL)
%
%
%   Example: 
%   
%   [NSx] = combineNSx('c:\data\saveddata1.ns5', 'c:\data\saveddata2.ns5');
%
%   The above example reads the two files (full path needed)
%   c:\data\saveddata1.ns5 and c:\data\saveddata2.ns5 and
%   combines them into a single variable NSx into MATLAB workspace.
%
%   Kian Torab
%   ktorab@blackrockmicro.com
%   Blackrock Microsystems
%
%   Version 1.1.2.0


function [NSx1] = combineNSx(filename1, filename2)

% Opening NSx files
if exist('filename1', 'var') && exist('filename2', 'var')
    disp('Load the first NSx file.');
    NSx1 = openNSx('read', filename1);
    disp('Load the second NSx file.');
    NSx2 = openNSx('read', filename2);
    if NSx1.MetaTags.SamplingFreq ~= NSx2.MetaTags.SamplingFreq
        disp('The sampling frequencies are not the same.');
        return;
    end
else
    NSx1 = openNSx('read');
    NSx2 = openNSx('read');
end

% Combining NSx files
NSx1.Data = [NSx1.Data, NSx2.Data];
